#include "Turret.h"
#include "BehaviorManager.h"
#include "BaseBehavior.h"
#include "CreateBulletMessage.h"
#include "Player.h"
#include "../SGD Wrappers/SGD_Event.h"
#include "../SGD Wrappers/SGD_AudioManager.h"


Turret::Turret() : Listener(this)
{
	RegisterForEvent("TARGET_FOUND");
	RegisterForEvent("LOST_TARGET");
	RegisterForEvent("STUNNED");
	RegisterForEvent("UNPAUSE");
	RegisterForEvent("PAUSE");
	turnSound = &GameplayState::GetInstance()->cameraRotate;
	voice = SGD::AudioManager::GetInstance()->PlayAudio(*turnSound, true);
	SGD::AudioManager::GetInstance()->SetVoiceVolume(voice, 0);
	m_FieldOfView.SetOwner(this);
	animation.m_strCurrAnimation = "turret";
	animation.m_fCurrDuration = 0.0f;
	animation.m_nCurrFrame = 0;

}


Turret::~Turret()
{
	UnregisterFromEvent("TARGET_FOUND");
	UnregisterFromEvent("LOST_TARGET");
	UnregisterFromEvent("STUNNED");
	UnregisterFromEvent("UNPAUSE");
	UnregisterFromEvent("PAUSE");
}


/*virtual*/ void Turret::Update(float dt)	 /*override*/
{
	if ((m_pTarget->GetPosition() - m_ptPosition).ComputeLength() < 895.0f && SGD::AudioManager::GetInstance()->IsVoiceValid(voice))
		SGD::AudioManager::GetInstance()->SetVoiceVolume(voice, 25);

	else if (SGD::AudioManager::GetInstance()->IsVoiceValid(voice))
		SGD::AudioManager::GetInstance()->SetVoiceVolume(voice, 0);

	if (m_tStunTimer.Update(dt))
	{
		if (currBehavior != nullptr)
		{
			if (m_nAIState == PATROL)
			{
				
				if (currBehavior->Update(dt, this, *navGraph->GetVertexElement(m_nCurrWaypoint)))
				{
					if (SGD::AudioManager::GetInstance()->IsVoiceValid(voice))
					{
						SGD::AudioManager::GetInstance()->StopAudio(*turnSound);
						//SGD::AudioManager::GetInstance()->StopVoice(voice);
						voice = SGD::AudioManager::GetInstance()->PlayAudio(*turnSound, true);
					}
					
					

					m_nCurrWaypoint++;
					if (m_nCurrWaypoint == m_vWayPoints.size())
						m_nCurrWaypoint = 0;
				}
			}
		
			else if (m_nAIState == ALERT)
			{
				currBehavior->Update(dt, this, m_pTarget->GetPosition());
				if (m_tShotTimer.Update(dt))
					Attack();

				
			}
			
		}
		EnemyUnit::Update(dt);
	}
}

/*virtual*/ void Turret::HandleEvent(const SGD::Event* pEvent)
{
	if (pEvent->GetEventID() == "LOST_TARGET")
	{
		m_nAIState = PATROL;
		RetrieveBehavior("rotate");

	}


	else if (pEvent->GetEventID() == "TARGET_FOUND")
	{
		m_nAIState = ALERT;
		
	}

	else if (pEvent->GetEventID() == "STUNNED")
		m_tStunTimer.AddTime(10);

	else if (pEvent->GetEventID() == "PAUSE")
	{
		if (SGD::AudioManager::GetInstance()->IsVoiceValid(voice))
			SGD::AudioManager::GetInstance()->PauseVoice(voice, true);
	}
	

	else if (pEvent->GetEventID() == "UNPAUSE")
	{
		if (SGD::AudioManager::GetInstance()->IsVoiceValid(voice))
			SGD::AudioManager::GetInstance()->PauseVoice(voice, false);

	}
	else if (pEvent->GetEventID() == ShutDownEvent.c_str())
	{
		//TODO: Handle computer shut down event

	}
}
/*virtual*/ void Turret::HandleCollision(const IBase* pOther) /*override*/
{

}

void Turret::RetrieveBehavior(std::string name)
{

	if (currBehavior != nullptr)
	{
		currBehavior = nullptr;
	}
	currBehavior = BehaviorManager::GetInstance()->GetBehaviors()[name];
}

/*virtual*/ void Turret::Attack() /*override*/
{
	m_tShotTimer.AddTime(0.1f);
	CreateBulletMessage* pMsg = new CreateBulletMessage(this);
	pMsg->QueueMessage();
	pMsg = nullptr;
}
